-- If user didn't specify a starting volume, use the current volume
if pBegin then
set pStart = the volume of sound pSoundChannel
end if
-- Jump abruptly to the starting volume if necessary
if the volume of sound pSoundChannel <> pStart then
set the volume of sound pSoundChannel = pStart
end if
-- Calculate how much to change the volume in each tick of time
set pChangePerTick = float(pEnd - pStart)/pDuration
end beginSprite
on endSprite me
-- Set the ending volume even if rounding errors left it at an inexact level
set the volume of sound pSoundChannel = pEnd
end endSprite
on exitFrame me
-- Fade the sound up or down between the starting and ending volumes.
-- There are several possible modes for waiting:
-- If pTight is TRUE, we'll create a tight repeat loop and fade the sound
-- If pTight is FALSE, we'll adjust the volume once per exitFrame event
-- If pWait is TRUE, we'll also wait in the frame until the sound fade completes
-- If pTight and pWait are both FALSE, the sound will fade for the number of
-- frames over which the script is tweened. When leaving the sprite span
-- the ending volume will may be set abruptly in that case.
if pTight then
-- pTight is TRUE, so we'll create a tight repeat loop and fade the sound
repeat while not done(me)
set elapsed = the ticks - pStartTime
set the volume of sound pSoundChannel = pStart + elapsed * pChangePerTick
end repeat
else
-- pTight is FALSE, so we'll adjust the volume once per exitFrame event
set elapsed = the ticks - pStartTime
set the volume of sound pSoundChannel = pStart + elapsed * pChangePerTick
-- If desired, wait in the frame until the final volume is achieved
if pWait and not done (me) then
go the frame
end if
end if
end exitFrame
on done me
-- Check if the sound fade is done.
-- Returns TRUE when we reach or pass the volume of interest
if (pStart > pEnd) and (the volume of sound pSoundChannel > pEnd) then
return FALSE
else if (pStart < pEnd) and (the volume of sound pSoundChannel < pEnd) then
return FALSE
else
return TRUE
end if
end done
on getBehaviorDescription me
set intro = "-- Sound Fade Behavior by Bruce A. Epstein. Copyright 1999. All rights reserved. " & RETURNs(1)
put "-- See Chapter 15 of Director in a Nutshell and http://www.zeusprod.com/nutshell/fade.html" & RETURNs(2) after INTRO
put "Parameters:" & RETURNs(1) after INTRO
set desc1 = "-- pSoundChannel - sound channel to fade (from 1 to 8). Only one channel can be faded at a time." & RETURNs(2)
set desc2 = "-- pBegin - if TRUE, fade begins from current volume, and pStart is ignored." & RETURNs(2)
set desc3 = "-- pStart - starting volume for fade (0-255). If pBegin is TRUE, this parameter is ignored. " & RETURNs(2)
set desc4 = "-- pEnd - ending volume for fade (0-255). Automatically fades up or down, depending on relation to pStart." & RETURNs(2)
set desc5 = "-- pDuration - number of ticks over which to create fade. Default is 180 ticks (3 seconds)." & RETURNs(2)
set desc6 = "-- pTight - if TRUE, sound fade is performed in a tight repeat loop, locking out animation and screen updates. If FALSE, sound is faded incrementally each time exitFrame event is sent." & RETURNs(2)
set desc7 = "-- pWait - if TRUE, wait in the frame until the sound fade completes. If pTight is FALSE and pWait is FALSE, the sound will change over the range of frames over which the script is tween. pWait is irrelevant if pTight is TRUE." & RETURNs(2)